

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Servlet implementation class ForGotPwd
 */
@WebServlet("/ForGotPwd")
public class ForGotPwd extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String username;
    private String pwd;
    private String email;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForGotPwd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		username = request.getParameter("userName");
		email = request.getParameter("email");
		List result = null;
		List result2= null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
			String sqlString = "select * from hostusers where account = '" + username + "' "
                    + "and emailAddress = '" + email + "'";
			Query queryResult = session.createSQLQuery(sqlString).addEntity(hostUser.class);;
			result = queryResult.list();
			if(result.isEmpty() || result == null){
				String sqlString2 = "select * from guestusers where account = '" + username + "' "
	                    + "and emailAddress = '" + email + "'";
				Query queryResult2 = session.createSQLQuery(sqlString2).addEntity(guestUser.class);;
				result2 = queryResult.list();
				if(result2.isEmpty() || result2 == null){
					out.println("<html><head><title>host Manager</title></head><body>");
		    		out.println("<a>sql 1403: no user exits</a>");
		    		out.println("</body></html>");
				}
				else{
					guestUser guestUserObj = new guestUser();
					guestUserObj = (guestUser) result2.get(0);
					pwd = guestUserObj.getPassword();
					System.out.println("password: " + pwd);
				}
			}
			else{
				hostUser hostUserObj = new hostUser();
				hostUserObj = (hostUser) result.get(0);
				pwd = hostUserObj.getPassword();
				System.out.println("password1: " + pwd);
			}
		}catch(Exception ex){
			HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
		}
	}

}
